import BigNumber from 'bignumber.js'
import { formatTime } from '@/utils/tools'
import { setParams, getParams } from '@/utils/jump'
import { mapState } from 'vuex'
const mixin = {
  data() {
    return {
      HEIGHT: document.documentElement.clientHeight
    }
  },
  computed: {
    ...mapState({
      USER: state => state.user.userInfo
    })
  },
  created() {
    window.onresize = () => {
      // 只要窗口高度发生变化，就会进入这里面，在这里就可以写，回到聊天最底部的逻辑
      this.HEIGHT = document.documentElement.clientHeight
      console.log('document.documentElement.clientHeight:', document.documentElement.clientHeight)
    }
  },
  methods: {
    setParams,
    getParams,
    getURLParams() {
      return this.$route.query
    },
    getProportion(v) {
      if (!v || v === '-') {
        return v
      }
      return BigNumber(v).toFixed(2) + '%'
    },
    formatMoney(v) {
      if (isNaN(v) || v === null || v === undefined || v === '') {
        return '-'
      }
      const fmt = {
        prefix: '',
        decimalSeparator: '.',
        groupSeparator: ',',
        groupSize: 3,
        secondaryGroupSize: 3
      }
      return BigNumber(v).toFormat(2, fmt)
    },
    formatTime,
    gotoInfoPage(path, id) {
      if (window.__MICRO_APP_ENVIRONMENT__) {
        //
        window.microApp.dispatch({ event: 'openTag', data: { url: '/wb-crm' + path, query: { id }}})
      } else {
        this.$router.push({ path, query: { id }})
      }
    },
    goMainBack() {
      if (window.__MICRO_APP_ENVIRONMENT__) {
        //
        console.log('goMainBack:', window.microApp)
        window.microApp.dispatch({ event: 'back', value: new Date().getTime() })
      } else {
        this.$router.back()
      }
    },
    gotoMainPage(path, query, params) {
      const baseRoute = window.__MICRO_APP_BASE_ROUTE__ || '/'

      const data = {
        path: baseRoute + path
      }
      if (query) {
        data.query = query
      }
      if (params) {
        data.params = params
      }
      if (window.__MICRO_APP_ENVIRONMENT__) {
        window.microApp.dispatch({ event: 'openTag', data: data })
      } else {
        this.$router.push(data)
      }
    }

  }
}
export default mixin
